package com.wlw.equipment.config;

import com.wlw.equipment.constants.MqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MQ配置类
 *
 * @author lutianqi
 * @date 2022/5/28 16:11
 */
@Configuration
public class MqConfig {
    @Bean   //声明交换机
    public TopicExchange topicExchange() {
        return new TopicExchange(MqConstants.EQUIPMENT_EXCHANGE, true, false);
    }

    @Bean   //声明新增、修改队列
    public Queue insertQueue() {
        return new Queue(MqConstants.EQUIPMENT_INSERT_QUEUE, true);
    }

    @Bean   //声明删除队列
    public Queue deleteQueue() {
        return new Queue(MqConstants.EQUIPMENT_DELETE_QUEUE, true);
    }

    @Bean   //声明绑定关系(equipment_INSERT_KEY 对应 insertQueue)
    public Binding inserQueueBinding() {
                            //调用insert队列方法，获得队列
        return BindingBuilder.bind(this.insertQueue())
                //调用交换机方法，获得交换机
                .to(this.topicExchange())
                //指定RoutingKey
                .with(MqConstants.EQUIPMENT_INSERT_KEY);
    }

    @Bean   //声明绑定关系(equipment_DELETE_KEY 对应 deleteQueue)
    public Binding deleteQueueBinding() {
                                //调用delete队列方法，获得队列
        return BindingBuilder.bind(this.deleteQueue())
                //调用交换机方法，获得交换机
                .to(this.topicExchange())
                //指定RoutingKey
                .with(MqConstants.EQUIPMENT_DELETE_KEY);
    }
}
